package com.contactmgr.utils;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.contactmgr.logObjects.CallDataLog;
import com.contactmgr.logObjects.Contact;
import com.contactmgr.logObjects.Sms;

public class ContactNameResolver {
	
	
	public static List<Sms> resolveNamesInSms(List<Contact> contact, List<Sms> sms) {
		// create lookup table

		for (Sms msg : sms) {
			String fromTo = msg.getFromTo();
			msg.setPerson("");
			for (Contact cont : contact) {
				// if current phone(any type) contains
				Collection aa = cont.getPhone().values();
				Iterator itr = aa.iterator();
				while (itr.hasNext()) {
					String phone = itr.next().toString();
					if (phone.endsWith(fromTo) || fromTo.endsWith(phone)) {
						msg.setPerson(cont.getDisplayName());
						System.out.println("Found person" + msg.getPerson());
						break;
					}
				}

			}
		}
		return sms;
	}

	public static List<CallDataLog> resolveNamesInCallLog(List<Contact> contact, List<CallDataLog> callData) {
		// create lookup table

		for (CallDataLog log : callData) {
			String fromTo = log.getNumber();
			log.setName("");
			for (Contact cont : contact) {
				// if current phone(any type) contains
				Collection aa = cont.getPhone().values();
				Iterator itr = aa.iterator();
				while (itr.hasNext()) {
					String phone = itr.next().toString();
					if (phone.endsWith(fromTo) || fromTo.endsWith(phone)) {
						log.setName(cont.getDisplayName());
						System.out.println("Found person" + log.getName());
						break;
					}
				}

			}
		}
		return callData;
	}

}
